<?php

if (!defined('BASE_PATH'))
    exit('No direct script access allowed');
/**
 * 忘道Php框架 
 * 
 * 作者 爱是小微笑 
 * 联系 QQ：549193902 EMAIL：549193902@qq.com
 * 2013-10-11 9:00:32
 */
// ------------------------------------------------------------------------
/**
 * 日志写入
 * 
 * @param type $level
 * @param type $content
 */
function write_log($level = 0, $content = 'none') {
    file_put_contents(APP . 'log/' . $level . '-' . date('Y-m-d') . '.log', $content, FILE_APPEND);
}

function log_message($level = 'error', $message, $php_error = FALSE) {
    //echo($message);
}

function is_show($var, $show = 'cur', $character = 'and') {

    if ($character === 'and') {
        $return = true;
        foreach ($var as $k => $v) {
            if ($k !== $v) {
                $return = false;
            }
        }
    } else {
        $return = false;
        $k = array_keys($var);
        $v = array_values($var);
        if(in_array($k[0], $v[0])){
            $return = true;
        }
    }
    if ($return) {
        return $show;
    }
    return '';
}

/**
 * 生成URL
 * 
 * @param type $path
 * @return type
 */
function site_url($path) {
    return BASE_URL . BASE_EXT . $path . '.' . BASE_SUFFIX;
}

/**
 * 生成静态文件路径
 * 
 * @param type $path
 * @param type $SYSTEM_PATH
 * @return type
 */
function static_url($path = '', $SYSTEM_PATH = true) {
    if ($SYSTEM_PATH) {
        return BASE_URL . 'static/' . $path;
    } else {
        $WD = & get_instance();
        return BASE_URL . APP_PATH . $WD->input->url('0') . '/static/' . $path;
    }
}

/**
 * 显示404
 * 
 */
function show_404() {
    $load = load_class("loader", 'core');
    header("HTTP/1.1 404 Not Found");
    $load->view('v/404');
    exit(1);
}

/**
 * 显示错误
 * 
 */
function show_error() {
    $load = load_class("loader", 'core');
    header("HTTP/1.1 500 Not Found");
    $load->view('v/404');
    exit(1);
}

/**
 * 生成毫秒为单位数
 * 
 * 用以统计计算运行时间
 * @return type
 */
function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float) $usec + (float) $sec);
}

/**
 * 单例加载
 * 
 * @staticvar array $objects
 * @param type $class 类名
 * @param type $directory 类型
 * @return array|\class
 */
function load_class($class, $directory = 'core') {
    static $objects = array();

    if (isset($objects[$class])) {
        return $objects[$class];
    }
    if (is_file(SYSTEM_PATH . $directory . '/' . $class . '.php')) {
        require_once SYSTEM_PATH . $directory . '/' . $class . '.php';
    } else {
        show_error();
    }
    $objects[$class] = new $class();
    return $objects[$class];
}

?>